package com.creditcmsplt.util;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * 绕过ssl证书校验
 * @author 雷阳军
 *
 */
public class SSLUtil {
	 private static final TrustManager[] UNQUESTIONING_TRUST_MANAGER = new TrustManager[] { new X509TrustManager() {
		@Override
		public java.security.cert.X509Certificate[] getAcceptedIssuers() {
			return null;
		}
		
		@Override
		public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
				throws CertificateException {
		}
		
		@Override
		public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
				throws CertificateException {
		}
	}};

	    public static void turnOffSslChecking() throws NoSuchAlgorithmException, KeyManagementException {
	        final SSLContext sc = SSLContext.getInstance("SSL");
	        sc.init(null, UNQUESTIONING_TRUST_MANAGER, null);
	        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
	    }

	    public static void turnOnSslChecking() throws KeyManagementException, NoSuchAlgorithmException {
	        SSLContext.getInstance("SSL").init(null, null, null);
	    }

	    private SSLUtil() {
	        throw new UnsupportedOperationException("Do not instantiate libraries.");
	    }
}
